﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Skin2D.Bone;
using Skin2D.Control.LogSystem;

namespace Skin2D.Control.Cursor
{
    class RotateCursor : ICursor
    {
        public RotateCursor(ToolStripButton button)
            : base(button)
        {
            this.Type = CurrsorType.Rotate;
        }

        public override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && m_selected != null)
            {
                m_last_point = new PointF(e.X, e.Y);
            }

        }

        public override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && m_selected != null)
            {
                PointF point = new PointF(e.X, e.Y);

                float translate_x = m_last_point.X - point.X;
                float translate_y = m_last_point.Y - point.Y;

                m_selected.setRotate(m_selected.RelativeRotation + translate_y);

                m_last_point = point;

                Program.form.Cursor = Cursors.SizeNS;
            }
        }

        public override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
                return;

            Program.form.Cursor = Cursors.Default;

            PointF point = new PointF(e.X, e.Y);
            this.TransfromToCamera(ref point);

            //Check Modify Last selected
            {
                if (m_selected !=null && m_rotate != -1)
                {
                    if (m_selected.Length != m_rotate)
                        EventLog.AddLog("RelativeRotation", m_selected, m_rotate);
                }
            }

            m_selected = null;
            m_rotate = -1;
            foreach (Bone.Bone2D item in MyApplication.m_BoneListTreeView.m_TreeNodeMap.Keys)
            {
                if (item.getBoundBox().Contains(point))
                {
                    MyApplication.m_BoneListTreeView.Select(item);
                    m_selected = item;
                    m_rotate = m_selected.RelativeRotation;
                    return;
                }
            }

            MyApplication.m_BoneListTreeView.Select(null);
        }

        private Bone2D m_selected;
        private PointF m_last_point;
        private float m_rotate;
    }
}
